package com.example.demoplus.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.demoplus.R;
import com.example.demoplus.adapter.MenuAdapter;
import com.example.demoplus.bean.ShopBean;

public class DashboardFragment extends Fragment implements View.OnClickListener {
    private ShopBean shopBean;
    private TextView tvHeadShopName, tvTime, tvNotice, tvTitle;
    private ImageView ivShopPic, ivBack;
    private ListView lvList;
    private MenuAdapter menuAdapter;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //        拿到从ShopAdapter中传来的数据
        shopBean = (ShopBean) getActivity().getIntent().getSerializableExtra("shopBean");
//        判断传递过来的数据是否为空
        if (shopBean == null){
//            如果为空，结束页面
            return;
        }
//        添加寻找控件方法
        initView(view);
//        适配器方法
        initAdapter();
//        设置数据
        initData();
    }

    private void initAdapter() {
//        初始化
        menuAdapter = new MenuAdapter(getContext());
//        绑定菜单数据
        lvList.setAdapter(menuAdapter);
    }

    //  寻找控件方法
    private void initView(View view) {
        tvHeadShopName = view.findViewById(R.id.tv_head_shop_name);
        tvTime = view.findViewById(R.id.tv_head_time);
        tvNotice = view.findViewById(R.id.tv_notice);
        tvTitle = view.findViewById(R.id.tv_title);
        ivShopPic = view.findViewById(R.id.iv_head_shop_pic);
        ivBack = view.findViewById(R.id.iv_back);
        lvList = view.findViewById(R.id.lv_list);
//        设置标题
        tvTitle.setText("店铺详情");
//        设置返回按钮可见
        ivBack.setVisibility(View.VISIBLE);
//        对返回按钮做监听 需要实现OnClickListener接口，并实现onClick方法
        ivBack.setOnClickListener(this);
    }
    //    设置数据
    private void initData() {
        tvHeadShopName.setText(shopBean.getShopName());
        tvTime.setText(shopBean.getTime());
        tvNotice.setText(shopBean.getShopNotice());
        Glide.with(getContext()).load(shopBean.getShopPic()).error(R.mipmap.ic_launcher).into(ivShopPic);
//        设置菜单食物数据
        menuAdapter.setData(shopBean.getFoodList());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            如果点击了
            case R.id.iv_back:
                break;
        }
    }
}